<template>
  <el-row class="page-product">
    <el-col :span="24">
      <crumbs :keyword="keyword" />
      <category
        :types="types"
        :areas="areas"
      />
      <list :list="list" />
    </el-col>
  </el-row>
</template>

<script>
import Crumbs from '@/components/products/crumbs.vue'
import Category from '@/components/products/category.vue'
import List from '@/components/products/list.vue'
export default {
  components: {
    Crumbs,
    Category,
    List
  },
  data() {
    return {
      list: [],
      types: [],
      areas: [],
      keyword: ''
    }
  },
  watch: {
    $route(newVal) {
      this.getData(newVal.query.keyword)
    }
  },
  async asyncData(ctx) {
    const keyword = ctx.query.keyword
    const city = ctx.store.state.geo.position.city
    const { status, data: { count, pois } } = await ctx.$axios.get('/search/resultsByKeywords', {
      params: {
        keyword,
        city
      }
    })
    const { status: status2, data: { areas, types } } = await ctx.$axios.get('/category/crumbs', {
      params: {
        city
      }
    })
    if (status === 200 && count > 0 && status2 === 200) {
      return {
        list: pois.filter(item => item.photos.length).map((item) => {
          return {
            type: item.type,
            img: item.photos[0],
            name: item.name,
            comment: Math.floor(Math.random() * 10000),
            rate: Number(item.biz_ext.rating),
            price: Number(item.biz_ext.cost),
            scene: item.tag,
            tel: item.tel,
            status: '可订明日',
            address: item.address,
            module: item.type.split(';')[0]
          }
        }),
        keyword,
        areas: areas.filter(item => item.type !== '').slice(0, 5),
        types: types.filter(item => item.type !== '').slice(0, 5),
        point: []
      }
    }
  },
  methods: {
    async getData(keyword) {
      const city = this.$store.state.geo.position.city
      const { status, data: { count, pois } } = await this.$axios.get('/search/resultsByKeywords', {
        params: {
          keyword,
          city
        }
      })
      if (status === 200 && count > 0) {
        this.keyword = keyword
        this.list = pois.filter(item => item.photos.length).map((item) => {
          return {
            type: item.type,
            img: item.photos[0],
            name: item.name,
            comment: Math.floor(Math.random() * 10000),
            rate: Number(item.biz_ext.rating),
            price: Number(item.biz_ext.cost),
            scene: item.tag,
            tel: item.tel,
            status: '可订明日',
            address: item.address,
            module: item.type.split(';')[0]
          }
        })
      }
    }
  }
}
</script>

<style lang="scss">
  @import "@/assets/css/products/index.scss";
</style>
